#!/usr/bin/perl

use Cwd;

use Data::Dumper;

$baseurl = getcwd;

print "baseurl: " . $baseurl . "\n";

use IO::File;

binmode(STDOUT, ':utf8');

open (STDERR, ">errors_Variables.txt");

$batch_textfiles = "expect_variables.txt";

if(-e $batch_textfiles) {
open (MYFILE, $batch_textfiles);

$backup_batch_textfiles = "expect_variables_bak.txt";

$backup_batch_loader = IO::File->new("> $backup_batch_textfiles") || die "cannot open $backup_batch_textfiles for writing: $!\n";
flock($backup_batch_loader, 2);

while (<MYFILE>) {

    print $backup_batch_loader $_;

chomp;
push(@variables_array, $_);

}
close (MYFILE); 

    $backup_batch_loader->autoflush(1);
     close($backup_batch_loader, 2) || "cannot close $backup_batch_loader: $!";

} else {

@variables_array = (
"ssh_spawn		#Enter the login for connecting to III server; make sure to include login@full.server.address", 
"ssh_pword		#Enter the password for the expect processess", 
"initials		#Enter initials of user authorized to create lists and export data", 
"pword		#Enter password of user authorized to create lists and export data", 
"email_address		#Enter the email addresses to receive progress reports", 
"seconds_previous	172800	#Enter the number of seconds in a 24 hour day time the number of days, e.g., 86400 x 1 = 86400; 86400 x 2 = 172800; 86400 x 3 = 259200; 86400 x 7 = 604800; 86400 x 30 = 2592000",
"bib_reviewnumber	#Enter the number of the review list to be used for bibliographic records; filesize should not be over 10000",
"bib_filesize	#Enter the size of the review list to be used for bibliographic records; filesize should not be over 10000",
"reviewnumber		#Enter the number of the review list to be used for all other records; filesize should be as close to 60000 as possible",
"filesize		#Enter the size of the review list for deleted record numbers; filesize should be as close to 60000 as possible",
"host_name		#Enter the host name of mysql server; include full server name", 
"host_user		#Enter the user name of the owner of the mysql tables", 
"host_pword		#Enter the password of mysql server"
);

}

$batch_loader = IO::File->new("> $batch_textfiles") || die "cannot open $batch_textfiles for writing: $!\n";
flock($batch_loader, 2);

foreach (@variables_array) {

    @parts = split("\t", $_);

    $value_name = $parts[0];

    $this_value = &get_variables($parts[2], $parts[1]);

    $this_value =~ s/^[\s]*|[\s]*$//g;

    print "\n";

    print $batch_loader $value_name . "\t" . $this_value . "\t" . $parts[2] . "\n";

}

    $batch_loader->autoflush(1);
    close($batch_loader, 2) || "cannot close $batch_loader: $!";


undef(@variables_array);

print "Now set up connect.pl file for indexers and MySQL loaders. \n\n";

$batch_textfiles = "connect.pl";

if(-e $batch_textfiles) {
open (MYFILE, $batch_textfiles);

$backup_batch_textfiles = "connect_bak.pl";

$backup_batch_loader = IO::File->new("> $backup_batch_textfiles") || die "cannot open $backup_batch_textfiles for writing: $!\n";
flock($backup_batch_loader, 2);

while (<MYFILE>) {

    print $backup_batch_loader $_;

chomp;

s/^\$//;
s/ = /\t/;
s/;/\t/;
s/"//g;

    if($_ !~ /^#|^$/) {
        push(@variables_array, $_);
    }

}

close (MYFILE); 

    $backup_batch_loader->autoflush(1);
    close($backup_batch_loader, 2) || "cannot close $backup_batch_loader: $!";

} else {

@variables_array = (
'db_name		#Enter MySQL database name', 
'db_host	localhost	#Enter host name', 
'db_user		#Enter MySQL user name', 
'db_pass		#Enter MySQL user password', 
'bib_filesize	#Enter the size of the review list to be used for bibliographic records; filesize should not be over 10000',
'filesize		#Enter the size of the review list for deleted record numbers; filesize should be as close to 60000 as possible',
'email_address		#Enter the email address of person(s) monitoring this project',
'digital_library	hathi	#Enter name of external digital library collection', 
'institution		#Enter institution 5 digit code, e.g., "mu3ug"', 
'consortium		#Enter consortium name. Leave blank if not part of a consortium', 
'xmlopac		#Enter "yes" if have rights to a III xmlopac. Leave blank if not available', 
'xmlopac_baseurl		#Enter baseurl of xmlopac if available, e.g., "http://holmes.lib.muohio.edu/xmlopac/". Leave blank if not available'
);

}


$batch_loader = IO::File->new("> $batch_textfiles") || die "cannot open $batch_textfiles for writing: $!\n";
flock($batch_loader, 2);

print $batch_loader "#!/usr/bin/perl \n\n";

foreach (@variables_array) {

    @parts = split("\t", $_);
    
    $value_name = $parts[0];

    $parts[1] =~ s/'//g;

    $this_value = &get_variables($parts[2], $parts[1]);

    $this_value =~ s/^[\s]*|[\s]*$//g;

    print "\n";

    print $batch_loader "\$" . $value_name . " = " . "\'" . $this_value . "\';" . $parts[2] . "\n";
    }

    $batch_loader->autoflush(1);
    close($batch_loader, 2) || "cannot close $batch_loader: $!";

sub get_variables {

   local($prompt,$default) = @_;

   if ($default ne '') {
      print $prompt, "\n[", $default, "]: ";
   } else {
      print $prompt, ": ";
   }

   $| = 1;               # force a flush after our print
   $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)

   chomp;

   if ("$default") {
      return $_ ? $_ : $default;    # return $_ if it has a value
   } else {
      return $_;
   }
}


